<?php
/**
 * Created by PhpStorm.
 * User: lee
 * Date: 2018/6/20
 * Time: 14:43
 */

namespace Biz\service\activity;

use Biz\model\activity\PopupList as mPopupList;
use Biz\model\activity\PopupLog as mPopUpLog;
use Biz\tools\Link;
use Biz\tools\ThrowException as toolThrowException;
use Doraemon\model\Member;
use Doraemon\model\VisitorMember as mVisitorMember;
use Doraemon\pockets\action\ApiAction;
use Doraemon\service\User;
use Doraemon\tools\ActionLog;

/**
 * @deprecated
 */
class Popup
{

    public static function getPopupInfo($uid, $isVisitor = false, $showType = 1, $time = '')
    {
        if (!$isVisitor) {
            $userInfo = User::getUser($uid);
        } else {
            $registerTime = mVisitorMember::getUserRegistrationTime($uid);
            $userInfo = [
                'type' => 0,
                'addtime' => $registerTime
            ];
        }
        $time = $time ? $time : date('Y-m-d H:i:s');
        $where = [
            'show_type' => $showType,
            'start_time[<=]' => $time,
            'stop_time[>=]' => $time
        ];
        $popupInfo = [];
        if ($userInfo['type'] == Member::AUTH_USER_BRAND) {//品牌用户不弹窗
            $popupInfo = [];
        } else {
            $info = [];
            $list = mPopupList::getList('*', $where, []);
            if ($list) {
                foreach ($list as $row) {
                    if ($row['show_rule'] == mPopupList::COUPON_SHOW_RULE_BY_REGISTER_TIME) {
                        if (round((strtotime($time) - $userInfo['addtime']) / 3600, 3) <= $row['show_times']) {
                            $info = $row;
                            break;
                        } else {
                            $info = [];
                        }
                    } else {
                        $info = $row;
                        break;
                    }
                }
            }
            $popUpId = $info['id'];
            if (!$isVisitor) {
                $logInfo = mPopUpLog::getLogs(['uid' => $uid, 'popup_id' => $popUpId]);
            } else {
                $logInfo = mPopUpLog::getLogs(['vid' => $uid, 'popup_id' => $popUpId]);
            }
            $logInfo = array_shift($logInfo);
            if ($info) {
                $jumpUrl = $info['link_url'];
                $linkId = $info['link_id'];
                if ($jumpUrl && $linkId) {
                    $isBase64 = false;
                    if ($linkId == Link::LINK_H5) {
                        $isBase64 = true;
                    }
                    $jumpUrl = Link::format($linkId, $jumpUrl, $isBase64, false);
                } else {
                    $jumpUrl = '';
                }
                $popupInfo = [
                    'popup_id' => $popUpId,
                    'img_url' => $info['banner_img_ori'],
                    'start_time' => $info['start_time'],
                    'stop_time' => $info['stop_time'],
                    'jump_url' => $jumpUrl,
                ];

                if ($logInfo) {
                    if ($info['coupon_id']) {//如果弹窗绑定优惠券
                        if ($logInfo['is_click']) {//并且点击领取过，则不弹窗
                            $popupInfo = [];
                        } else if (date('Y-m-d', strtotime($logInfo['edittime'])) == date('Y-m-d', strtotime($time))) {//当天弹过，不弹
                            $popupInfo = [];
                        }
                    } else {//如果不是优惠券并且弹过，则以后不弹窗
                        $popupInfo = [];
                    }
                }
            }
        }
        return $popupInfo;
    }

    public static function addPopUpLog($uid, $popUpId, $isVisitor = false)
    {
        if ($uid) {
            $result = mPopUpLog::addLog($uid, $popUpId, $isVisitor);
            return $result;
        }
        return false;
    }

    public static function clickPopUp($uid, $popUpId, $isVisitor = false)
    {
        if ($uid) {
            $where = [
                'uid' => $uid,
                'popup_id' => $popUpId
            ];
            if ($isVisitor) {
                $where = [
                    'vid' => $uid,
                    'popup_id' => $popUpId
                ];
            }
            $ret = mPopUpLog::updateLog($where, ['is_click' => 1]);
            $info = mPopupList::getPopupInfo($popUpId);
            if ($info['coupon_id'] && $info['coupon_ids']) {//领取优惠券
                $result = [];
                foreach ($info['coupon_ids'] as $couponId) {
                    ActionLog::log($uid, [
                        'tripper_id' => $isVisitor ? $uid : 0,
                        'is_login' => $uid ? 1 : 0,
                        'alert_id' => $popUpId,
                        'obj_id' => $couponId
                    ], ActionLog::BIZ_ALERT_MALL);
                    try {
                        $result[] = Coupon::receive($uid, $couponId);
                    } catch (\Exception $e) {

                    }
                }
                if ($isVisitor) {//游客领券，强制登录
                    toolThrowException::main(ApiAction::API_CODE_NEED_LOGIN, ApiAction::DEFAULT_LOGIN_COPY_WRITING);
                }
                if (count($result) > 0) {
                    return ['msg' => '领取成功', 'result' => true];
                } else {
                    toolThrowException::main(-1, '已领过啦');
                }
            }
        }
        return ['msg' => '', 'result' => true];
    }

}